<?php
// pr($charts);
?>

		<div id="portlet-template" class="hidden">
		        <div class="title"></div>
		        <div class="content">
					<div class="chart" style='height:90%;width:90%;' ></div>
		        </div>
		</div>
		<div class="row-fluid pull-left" style="padding-top: 20px;" >
			<div id="container1" class="container span4 pull-left" style="margin-left: 1.5%;">
			</div>
			<div id="container2" class="container span4 pull-left" style="margin-left: 1.5%;">
			</div>
			<div id="container0" class="container span4 pull-left" style="margin-left: 1.5%;">
			</div>
		</div>

<script>

	$(document).ready(function(){

	    $("#container1,#container2,#container0").sortable({
	            handle: ".title",
	            placeholder: "placeholder",
	            connectWith: ".container"
	    });
	    $("#container1,#container2,#container0").disableSelection();
	    refreshCharts();
		setFlash("Caricamento grafici eseguito");
	});

    function refreshCharts() {
    	var url = '<?php echo $this->webroot;?>Chart/refresh';
        $.getJSON( url, function( data ) {
           if(!data.success){
          		setFlash(data.message,data.message_level);
           } else {
       			// rimuovo tutti i portlet
	   			$('.portlet').remove();
                // console.time("check");
           		if (data.charts.length>0) {
           			for (var i=0; i<data.charts.length; i++) {
           				writePortlet(data.charts[i],'container'+(i%3)); //+(i%3)
					    drawChart(data.charts[i]['Chart']['code'],data.charts[i]['Chart']['code'],'');
           			}
           		} else {
					setFlash("Nessun chart configurato.");
           		}
	            $('#loading').hide();
           }
         });
    }


	function writePortlet(chart, container) {
		console.log(container);
		portletElement = $('#portlet-template').clone();
		portletElement.removeAttr('id','');
		portletElement.addClass('portlet');
		// portletElement.children('div.thumb').css('background','url('+webroot+chart.Notifica.thumb+')');
		// divTitleElement = portletElement.find('div');
		portletElement.children('div.title').html(chart.Chart.descr);
		divContent = portletElement.children('div.content');
		divContent.children('div.chart').attr('id',chart.Chart.code);
		portletElement.removeClass('hidden');
		portletElement.appendTo($('#'+container));
	}
    



</script>